<?php

namespace app\api\controller\wanlshop;

use app\common\controller\Api;
use fast\Tree;
/**
 * WanlShop店铺入驻
 */
class Entry extends Api
{
    protected $noNeedLogin = ['getShopInfo'];
    protected $noNeedRight = ['*'];
	
	public function _initialize()
	{
	    parent::_initialize();
		$this->model = new \app\index\model\wanlshop\Auth;
		// 获取用户下的申请
        $this->entry = $this->model->where(['user_id' => $this->auth->id])->find();
	}
	
	/**
	 * 申请入驻
	 *
	 * @ApiSummary  (WanlShop 申请入驻)
	 * @ApiMethod   (POST)
	 *
	 * @param string $id 页面ID
	 */
	public function index()
	{
		
		$data = input();
		if (!$data) {
			$this->error('参数错误');
		}
		if ($data['step'] == 1) {
			//第一步：提交资质
			$data['verify'] = '1';
			$data['user_id'] = $this->auth->id;
			$result = $this->entry ? $this->entry->allowField(true)->save($data) : $this->model->allowField(true)->save($data);
			$this->success('提交资质成功');
		}
		if ($data['step'] == 2) {
			//第二步：提交店铺
			if($this->entry){
				if ($this->entry->verify == '2' || $this->entry->verify == '3') {
				   // $this->error('保存失败');
				}
				$config = get_addon_config('wanlshop');
	            $verify = $config['config']['store_audit'] == 'N' ? 3:2;
	            // 更新提交信息
	            $data['user_id'] = $this->auth->id;
	            $data['verify'] = $verify;
	            if ($data['password']) {
	            	$this->auth->changepwd($data['password'], '', true);
	            }
	            unset($data['password']);
				$this->entry ? $this->entry->allowField(true)->save($data) : $this->model->allowField(true)->save($data);
				// 自动审核
				if($config['config']['store_audit'] == 'N'){
				    $row = model('app\index\model\wanlshop\Auth')->where(['user_id' => $this->auth->id])->find();
					// 新增店铺
					$shop = model('app\index\model\wanlshop\Shop');
					$shop->user_id = $this->auth->id;
					$shop->state = $row['state'];
					$shop->shopname = $row['shopname'];
					$shop->avatar = $row['avatar'];
					$shop->bio = $row['content'];
					$shop->description = $row['bio'];
					$shop->city = $row['city'];
					$shop->verify = $verify;
					// 新增店铺配置
					if($shop->save()){
						$config = model('app\index\model\wanlshop\ShopConfig');
						$config->shop_id = $shop->id;
						$config->save();
					}
				}
				$this->success('提交店铺成功');
			}
		}
	}

	/**
	 * 入驻信息
	 *
	 * @ApiSummary  (WanlShop 入驻信息)
	 * @ApiMethod   (POST)
	 *
	 * @param string $id 页面ID
	 */
	public function detail()
	{
		$uid = $this->auth->id;
		$row = model('app\index\model\wanlshop\Auth')->where(['user_id' => $uid])->find();
		//$expi = explode(',',$row->company_qualification);
		// foreach ($expi as &$vv) {
		// 	$vv = cdnurl($vv,true);
		// }
		// unset($vv);
		// $row->company_qualification_full = $expi;
		// $row->brand_qualification_full = cdnurl($row->brand_qualification,true);
		// $row->product_qualification_full = cdnurl($row->product_qualification,true);

		$this->success('返回成功', ["row" => $row]);
	}
	
	
}